package cn.lanqiao.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.lanqiao.entity.ApiResult;
import cn.lanqiao.entity.Score;
import cn.lanqiao.service.impl.ScoreServiceImpl;
import cn.lanqiao.util.ApiResultHandler;

@RestController
//跨域
@CrossOrigin(origins="*",maxAge=3600)
public class ScoreController {
    @Autowired
    private ScoreServiceImpl scoreService;

    @GetMapping("/scores")
    public ApiResult findAll() {
        List<Score> res = scoreService.findAll();
        return ApiResultHandler.buildApiResult(200,"查询所有学生成绩",res);
    }
//    分页
    @GetMapping("/score/{page}/{size}/{studentId}")
    public ApiResult findById(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @PathVariable("studentId") Integer studentId) {
        Page<Score> scorePage = new Page<>(page, size);
        IPage<Score> res = scoreService.findById(scorePage, studentId);
        return ApiResultHandler.buildApiResult(200, "根据ID查询成绩", res);
    }

//    不分页
    @GetMapping("/score/{studentId}")
        public ApiResult findById(@PathVariable("studentId") Integer studentId) {
        List<Score> res = scoreService.findById(studentId);
        if (!res.isEmpty()) {
            return ApiResultHandler.buildApiResult(200, "根据ID查询成绩", res);
        } else {
            return ApiResultHandler.buildApiResult(400, "ID不存在", res);
        }
    }
    @PostMapping("/score")
    public ApiResult add(@RequestBody Score score) {
        int res = scoreService.add(score);
        if (res == 0) {
            return ApiResultHandler.buildApiResult(400,"成绩添加失败",res);
        }else {
            return ApiResultHandler.buildApiResult(200,"成绩添加成功",res);
        }
    }

    @GetMapping("/scores/{examCode}")
    public ApiResult findByExamCode(@PathVariable("examCode") Integer examCode) {
        List<Score> scores = scoreService.findByExamCode(examCode);
        return ApiResultHandler.buildApiResult(200,"查询成功",scores);
    }
    
    @GetMapping("/subject/{subject}")
    public ApiResult findBySubject(@PathVariable("subject") String subject) {
    List<Score> res = scoreService.findBySubject(subject);
    if (!res.isEmpty()) {
        return ApiResultHandler.buildApiResult(200, "根据科目查询成绩", res);
    } else {
        return ApiResultHandler.buildApiResult(400, "科目不存在", res);
    }
}
    @GetMapping("/avg")
    public ApiResult findByAve() {
    List<Score> res = scoreService.findByAvg();
    if (!res.isEmpty()) {
        return ApiResultHandler.buildApiResult(200, "平均成绩", res);
    } else {
        return ApiResultHandler.buildApiResult(400, "科目不存在", res);
    }
}
    @GetMapping("/max/{subject}")
    public ApiResult findByMax(@PathVariable("subject") String subject) {
    List<Score> res = scoreService.findByMax(subject);
    if (!res.isEmpty()) {
        return ApiResultHandler.buildApiResult(200, "最高分", res);
    } else {
        return ApiResultHandler.buildApiResult(400, "科目不存在", res);
    }
}
    
    @GetMapping("/min/{subject}")
    public ApiResult findByMin(@PathVariable("subject") String subject) {
    List<Score> res = scoreService.findByMin(subject);
    if (!res.isEmpty()) {
        return ApiResultHandler.buildApiResult(200, "最低分", res);
    } else {
        return ApiResultHandler.buildApiResult(400, "科目不存在", res);
    }
}
    @GetMapping("/pass/{subject}")
    public ApiResult findByPass(@PathVariable("subject") String subject) {
    List<Score> res = scoreService.findByPass(subject);
    if (!res.isEmpty()) {
        return ApiResultHandler.buildApiResult(200, "通过率", res);
    } else {
        return ApiResultHandler.buildApiResult(400, "科目不存在", res);
    }
}
    @GetMapping("/value")
    public ApiResult findByValue() {
    List<Score> res = scoreService.findByValue();
    if (!res.isEmpty()) {
        return ApiResultHandler.buildApiResult(200, "最低成绩和最高成绩", res);
    } else {
        return ApiResultHandler.buildApiResult(400, "科目不存在", res);
    }
}
}
